package com.ews.onlineexchange.controller;

import java.util.List;

import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.ews.onlineexchange.model.OfferPrice;
import com.ews.onlineexchange.model.ServiceSupplier;
import com.ews.onlineexchange.model.Trade;
import com.ews.onlineexchange.service.EntityQuery;
import org.apache.commons.lang3.StringUtils;

@Model
public class OfferPriceHistoryList extends EntityQuery<OfferPrice> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Inject
	BulletinHome3 bulletinHome3;

	@Override
	protected List<Predicate> getRestrictions( CriteriaBuilder cb, Root<OfferPrice> entity ) {
		List<Predicate> restrictions = super.getRestrictions( cb, entity );
		String cnname = (String)getParameterValues().get("cnname");
		if(StringUtils.isNotBlank(cnname)){
			Join<Trade, ServiceSupplier> supplier = entity.join("trade").join("supplier");
			restrictions.add(cb.like(supplier.<String>get("cnname"),getStringRestriction(cnname)));
		}
		restrictions.add( cb.equal( entity.join( "trade" ).join( "bulletin" ).get( "id" ), bulletinHome3.getInstance().getId() ) );
		
		return restrictions;
	}

	public OfferPriceHistoryList() {
		setMaxResults(null);
		setOrder("createDate desc, id ASC");
	}

}
